package com.example.seven.mynews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.seven.mynews.R;
import com.example.seven.mynews.data.News;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by seven on 16-7-4.
 */
public class SimpleAdapter extends RecyclerView.Adapter<MyViewHolder>{

    private LayoutInflater inflater;
    private Context context;
    private List<News> mData;
    private OnItemClickListener listener;


    public SimpleAdapter(Context context,List<News> mData){
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建viewHolder;
        View view = inflater.inflate(R.layout.item_simple, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        //绑定viewHolder
        holder.textView.setText(mData.get(position).getTitle());//设置新闻标题
       //调用接口回调
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=listener){
                    listener.onItemClick(position,mData.get(position));
                }
            }
        });
    }

    /**
     * 内部接口回调方法
     */
    public interface OnItemClickListener{
        void onItemClick(int position,Object object);
    }

    /**
     * 设置监听方法
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener =listener;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder{

    TextView textView;

    public MyViewHolder(View itemView) {
        super(itemView);

        textView = (TextView) itemView.findViewById(R.id.tv);
    }
}
